package cn.dgut.training.utils;

import cn.dgut.training.vo.CommonResponse;
import com.google.gson.Gson;
import com.mysql.cj.QueryResult;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class RestUtil {

    public static Object getObject(RestTemplate restTemplate, String url, Class classOfT) {
        CommonResponse commonResponse = restTemplate.getForObject(url, CommonResponse.class);
        Gson gson = new Gson();
        Object object = new Object();
        if (commonResponse.getData() != null) {
            String s = gson.toJson(commonResponse.getData());
            object = gson.fromJson(s, classOfT);
        }
        return object;
    }

    public static Object getObject(CommonResponse commonResponse,Class classOfT){
        Gson gson = new Gson();
        Object object = new Object();
        if (commonResponse.getData() != null) {
            String s = gson.toJson(commonResponse.getData());
            object = gson.fromJson(s, classOfT);
        }
        return object;
    }


    public static CommonResponse postObject(RestTemplate restTemplate, String url, String postData) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> formEntity = new HttpEntity<String>(postData, headers);
        return restTemplate.postForObject(url, formEntity, CommonResponse.class);
    }
}
